package com.huayue.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.RoomPredictOrder;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 退房
 */
@Component
public class RoomCheckOutDao implements BaseDao<RoomPredictOrder> {

    /**
     * 根据房间id查询订单信息
     * 
     * @param id
     * @return
     */
    public Map<String, Object> findRoomPredictOrderByRoomId(Integer id) {
        String sql = " SELECT r.id,rom.name roomName,rt.name roomType,r.gmt_create real_in_date,\n"
            + " c.realname cname,rr.price predictMoney,rd.price deposit,r.real_out_time real_out_date, "
            + " r.ordernumber,rr.days,rs.in_time intime,rs.out_time outtime from room_real_order_details rr \n"
            + " LEFT JOIN room_real_order r on rr.room_real_order_id = r.id \n"
            + " left join customer c on rr.customer_id=c.id left join room rom on rr.room_id=rom.id\n"
            + " left join room_type rt on rom.room_type_id=rt.id\n"
            + " left join room_deposit rd on rd.room_real_order_id=r.id\n"
            + " left join room_status rs on rs.room_id=rr.room_id  where rr.room_id = ?  order by rr.id desc limit 0,1 ";
        return DBHelper.executeQuery(sql, id).get(0);
    }

    /**
     * 根据订单id查询该订单下的所有房间详情
     * 
     * @param orderId
     * @param RoomId
     * @return
     */
    public List<Map<String, Object>> findPredictOrderDetailsByRoomIdAndOrder(String orderId, Integer RoomId) {

        List<Map<String, Object>> finalList = new ArrayList<Map<String, Object>>();
        String sqlfirst =
            "select r.id,r.name,rrod.is_status,rrod.price,rrod.change_id,rrod.gmt_create crdate,rrod.gmt_modified chdate from room_real_order rro \n"
                + "right join room_real_order_details rrod on rrod.room_real_order_id=rro.id\n"
                + "left join room r on r.id=rrod.room_id where rro.id=? and rrod.room_id=?";
        finalList.add(DBHelper.executeQuery(sqlfirst, orderId, RoomId).get(0));
        String sql =
            "select r.id,r.name,rrod.is_status,rrod.price,rrod.change_id,rrod.gmt_create crdate,rrod.gmt_modified chdate from room_real_order rro \n"
                + "right join room_real_order_details rrod on rrod.room_real_order_id=rro.id\n"
                + "left join room r on r.id=rrod.room_id where rro.id=? and rrod.change_id=?";
        // 若无转房操作直接退出
        Long status = (Long)finalList.get(0).get("is_status");
        if (0 == status) {
            return finalList;
        }
        List<Map<String, Object>> first = DBHelper.executeQuery(sql, orderId, RoomId);
        if (first.size() == 0) {
            return finalList;
        }
        finalList.add(first.get(0));
        for (;;) {
            first = DBHelper.executeQuery(sql, orderId, first.get(0).get("id"));
            if (first.size() == 0) {
                break;
            }
            finalList.add(first.get(0));
        }
        return finalList;
    }

    /**
     * 退还押金
     * 
     * @param roomId
     * @return
     */
    public int updateDeposit(Integer roomId, Integer uid) {
        String sql = "update room_deposit set is_return = 1,employee_id_expend=? where id = (select d.id from "
            + " (select id from room_deposit rd where rd.room_id = ?  ORDER BY id desc limit 1) d)";
        return DBHelper.executeUpdate(sql, uid, roomId);
    }

    /**
     * 退还剩余预付金
     * 
     * @param roomId
     * @param amount
     * @return
     */
    public int updateRealDetails(Integer roomId, BigDecimal amount) {
        RoomChangeDao dao = new RoomChangeDao();
        int detailId = dao.findGenRoomDetailByRoomId(roomId);
        String sql = "update room_real_order_details set total_price = total_price-? where id=?";
        return DBHelper.executeUpdate(sql, amount, detailId);
    }

    /**
     * 根据房间id查询预付款
     * 
     * @return
     */
    public BigDecimal findPredictPriceByRoomId(Integer newRoomId) {
        RoomChangeDao dao = new RoomChangeDao();
        int detailId = dao.findGenRoomDetailByRoomId(newRoomId);
        // 根据id查询预付款
        String sql = "select total_price from room_real_order_details where id=?";
        return new BigDecimal(DBHelper.executeQuery(sql, detailId).get(0).get("total_price").toString());
    }

    /**
     * 将与该房间绑定的所有用户取消关联
     * 
     * @param roomId
     * @return
     */
    public int updateCustomerRoomId(Integer roomId) {
        String sql = "update customer set room_id=null where room_id=?";
        return DBHelper.executeUpdate(sql, roomId);
    }

    @Override
    public int insert(RoomPredictOrder entity) {
        return 0;
    }

    @Override
    public int update(RoomPredictOrder entity) {
        return 0;
    }

    @Override
    public int delete(RoomPredictOrder entity) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return null;
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        return null;
    }

    @Override
    public Map<String, Object> findOne(RoomPredictOrder entity) {
        return null;
    }

    @Override
    public int count(Object... obj) {
        return 0;
    }
}
